from django.shortcuts import render
from django.http import HttpResponse
from DLT.getweb import _to_sqlite
from DLT import general_and_constant
from DLT import models
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q



# Create your views here.

# 返回各期数据
def history_data(request):
    _to_sqlite('phasenum')
    lottery = models.phase_data.objects.all().order_by("-date")[0:20]  # 获取最新100期数据
    bonus_info = general_and_constant.query_recent_bonue_info()
    return render(request, 'DLT/history_data.html', {'lottery': lottery,'bonus_info':bonus_info})


def check_bonus(request):
    if request.method == "POST":
        if not ((request.POST.get("submit_check")) is None):
            phase_num = request.POST.get("note_phase")

            try:
                phaseinfo = models.phase_data.objects.get(phasenum=phase_num)  # get如查询不到，会抛出doesnoexist异常
            except ObjectDoesNotExist:
                return render(request, 'DLT/check_bonus.html',
                              {'error_info': '该期开奖数据不存在，可能还未开奖', 'phase_num': phase_num})
            lottery = general_and_constant.check_bonus(phase_num)
            note_num = len(lottery)
            first_price = phaseinfo.firstprice
            second_price = phaseinfo.secondprice
            total_bonus = general_and_constant.calculate_bonus(lottery, first_price, second_price)
            lottery = models.note_data.objects.filter(~Q(bonuslevel='未中奖'), phasenum=phase_num)
            return render(request, 'DLT/check_bonus.html',
                          {'lottery': lottery, 'firstbonus': first_price, 'secondbonus': second_price,
                           'total_bonus': total_bonus, 'phase_num': phase_num, 'note_num': note_num,
                           'win_note_num': len(lottery)})
        if not ((request.POST.get("submit_all_check")) is None):
            lottery = models.note_data.objects.filter(~Q(bonuslevel='未中奖'))
            return render(request, 'DLT/check_bonus.html', {'lottery': lottery, })

    return render(request, 'DLT/check_bonus.html', {})
    # return  HttpResponse('test')


def note(request):
    if request.method == "GET":
        return render(request, 'DLT/note.html')
    if request.method == "POST":
        if request.is_ajax():
            red1 = request.POST.getlist("reda[]")  # reda后面一定要夹加[]，否则无法获取数组
            blue1 = request.POST.getlist("bluea[]")
            red1.sort()
            blue1.sort()
            phasenum = request.POST.get("phasenum")
            notelist = general_and_constant.combinations_notes(red1, blue1)
            general_and_constant.save_notedata_to_DB(phasenum, notelist)
        return HttpResponse('ok')
    return render(request, 'DLT/note.html')


# 普通选号
def note_common(request):
    if request.method == "POST":
        if request.is_ajax():
            red1 = request.POST.getlist("reda[]")  # reda后面一定要夹加[]，否则无法获取数组
            blue1 = request.POST.getlist("bluea[]")
            red1.sort()
            blue1.sort()
            phasenum = request.POST.get("phasenum")
            notelist = general_and_constant.combinations_notes(red1, blue1)
            general_and_constant.save_notedata_to_DB(phasenum, notelist)
        return HttpResponse('ok')


# 胆拖选号
def note_dantuo(request):
    if request.method == "POST":
        if request.is_ajax():
            reddan = request.POST.getlist("reddan[]")  # reda后面一定要夹加[]，否则无法获取数组
            bluedan = request.POST.getlist("bluedan[]")
            redtuo = request.POST.getlist("redtuo[]")
            bluetuo = request.POST.getlist("bluetuo[]")
            reddan.sort()
            redtuo.sort()
            bluedan.sort()
            bluetuo.sort()
            phasenum = request.POST.get("phasenum")
            notelist = general_and_constant.dantuo_combinations_notes(reddan, redtuo, bluedan, bluetuo)
            general_and_constant.save_notedata_to_DB(phasenum, notelist)
        return HttpResponse('ok')
